package com.dark;

import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;

/**
 * Created by tengxue on 16-6-14.
 * 字符串的处理示例
 */
public class DealWithStringDemo {
    public static void main(String[] args){
        Joiner joiner = Joiner.on("; ").skipNulls();
        String result=joiner.join("Harry", null, "Ron", "Hermione");

        System.out.println("the joiner result is :------> " + result);

        String str="xxxx-control-aaaa";
        String noControl = CharMatcher.JAVA_ISO_CONTROL.removeFrom(str); //移除control字符

        System.out.println("the noControl result is :------> " + noControl);

        str="xxxxxxxx-111-2-zzzzzzzz";

        String theDigits = CharMatcher.DIGIT.retainFrom(str); //只保留数字字符
        System.out.println("the theDigits result is :------> " + theDigits);

        str=" xxxxxxxx  111 2 zzzzzzzz  2222 ";
        String spaced = CharMatcher.WHITESPACE.trimAndCollapseFrom(str, '-');//去除两端的空格，并把中间的连续空格替换成单个空格

        System.out.println("the spaced result is :------> " + spaced);

        str="xxxxxxxx-111-2-zzzzzzzz--2222";
        String noDigits = CharMatcher.JAVA_DIGIT.replaceFrom(str, "*"); //用*号替换所有数字

        System.out.println("the noDigits result is :------> " + noDigits);

        str="XXXXXX-111-22-zzzzzzzz--2222";
        String lowerAndDigit = CharMatcher.JAVA_DIGIT.or(CharMatcher.JAVA_LOWER_CASE).retainFrom(str); // 只保留数字和小写字母

        System.out.println("the lowerAndDigit result is :------> " + lowerAndDigit);
    }
}
